#!/bin/bash
set -e

TMP=${TMP:-/tmp/work}
LOG=${LOG:-$HOME/logs/stage1}
SRC=${SRC:-$LFS/sources}

name=ncurses
version=6.2

rm -fr $TMP
mkdir -pv $TMP $LOG

tar xf $SRC/$name-$version.tar.gz -C $TMP

{ time \
   {
    
    cd $TMP/$name-$version

    sed -i s/mawk// configure

    mkdir build
    pushd build
        ../configure
        make -C include
        make -C progs tic
    popd

    ./configure --prefix=/usr                \
                --host=$LFS_TGT              \
                --build=$(./config.guess)    \
                --mandir=/usr/share/man      \
                --with-manpage-format=normal \
                --with-shared                \
                --without-debug              \
                --without-ada                \
                --without-normal             \
                --enable-widec

    make

    make DESTDIR=$LFS TIC_PATH=$(pwd)/build/progs/tic install
    echo "INPUT(-lncursesw)" > $LFS/usr/lib/libncurses.so

    mv -v $LFS/usr/lib/libncursesw.so.6* $LFS/lib

    ln -sfv ../../lib/$(readlink $LFS/usr/lib/libncursesw.so) $LFS/usr/lib/libncursesw.so

    }
} 2>&1 | tee $name.log

[ $PIPESTATUS = 0 ] && mv $name.log $LOG || exit $PIPESTATUS

rm -fr $TMP
